package com.wl.index.web;

import com.wl.bean.Result;
import com.wl.dm.entity.Product;
import com.wl.dm.entity.User;
import com.wl.index.web.feign.OtherAction;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;

import java.util.List;

@RestController
public class IndexDo {
    @RequestMapping("index")
    String index(HttpServletRequest request) {
        return "index" + request.getServerPort();
    }

    @Autowired
    RestTemplate restTemplate;
    @RequestMapping("index-other")
    String other(){
        RestTemplate restTemplate = new RestTemplate();
        return restTemplate.getForObject("http://cloud-other/other", String.class);
    }

    @Autowired
    OtherAction otherAction;

    @RequestMapping("hots")
    List<Product> queryHots(){
        return otherAction.queryHots();
    }

    /// ///////////////////////////
    @RequestMapping("add")
    //服务端   属于在远程调用中被调用的这一端
    int add(@RequestParam("a") int a,
            @RequestParam("b") int b) {
        return otherAction.add(a,b);
    }

    //对象的属性映射为请求参数名，这就是自动装箱
    @RequestMapping("res")
    Result res(Result result) {
        return otherAction.res(result);
    }

    //用户信息是通过请求体body传递过来的
    @RequestMapping("user")
    User user(@RequestBody User user,
              @RequestParam("myname") String myname) {
        return otherAction.user(user, myname);
    }

    //Restful（一般写在地址里面，地址参数） 请求方式
    @RequestMapping("/hello/{nm}")
    String hello(@PathVariable("nm") String name) {
        return otherAction.hello(name);
    }
}
